﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Command;
using WP7Client.ServerManager;
using WP7Client.Helpers;
using MSH.Common.Classes;

namespace WP7Client.Controller
{
    public class ResendActivationController : BaseController
    {
        private String _MobilePhoneNumber;

        #region Get&Set

        public ICommand ReactivateCommand
        {
            get;
            internal set;
        }

        public String MobilePhoneNumber
        {
            get { return _MobilePhoneNumber; }
            set { _MobilePhoneNumber = value; }
        }

        #endregion

        public ResendActivationController()
        {
            ReactivateCommand = new RelayCommand(ReactivateExecute);
        }

        /// <summary>
        /// Vraag een nieuwe activatiecode op
        /// </summary>
        private void ReactivateExecute()
        {
            if (String.IsNullOrEmpty(MobilePhoneNumber))
            {
                GlobalHelper.ShowMessage("Error", "Mobile number needs to be filled.");
                return;
            }

            Loading = true;
            HttpGet activateHttpGet = new HttpGet(String.Format(GlobalHelper.Url + "ResendActivationCode/{0}/{1}/{2}", UserHelper.Username, UserHelper.Password, MobilePhoneNumber), typeof(ResendActivationResultEnum));
            activateHttpGet.FinishedGet += activateHttpGet_FinishedGet;
        }

        private void activateHttpGet_FinishedGet(object sender, EventArgs e) 
        {
            try 
            {
                HttpGet get = sender as HttpGet;
                if (get == null || get.resultObject == null)
                    return;

                ResendActivationResultEnum result = (ResendActivationResultEnum)get.resultObject;

                switch (result)
                {
                    case ResendActivationResultEnum.AlreadyActivated:
                        GlobalHelper.ShowMessage("Error", "Your account has already been activated.");
                        break;
                    case ResendActivationResultEnum.Failure:
                        GlobalHelper.ShowMessage("Error", "Failure due to unknown error.");
                        break;
                    case ResendActivationResultEnum.Success:
                        NavigationHelper.NavigateTo("/UserInterface/Pages/ActivationPage.xaml");
                        break;
                }
            }
            finally 
            { 
                Loading = false; 
            }
        }
        

    }
}
